package com.tyanxie.asd.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.tyanxie.asd.databinding.ProgressDialogBinding

/**
 * 加载框Fragment
 *
 * @param resId 消息文本资源id
 * @param formatArgs 消息文本格式化内容
 */
class ProgressDialogFragment(private val resId: Int, vararg formatArgs: Any?) : DialogFragment() {
    companion object {
        const val TAG = "DataPanelDialogFragment"
    }

    private val formatArgs = mutableListOf<Any?>()

    init {
        if (formatArgs.isNotEmpty()) {
            formatArgs.forEach { this.formatArgs.add(it) }
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        // 设置对话框不能取消
        requireDialog().setCancelable(false)
        // 设置点击对话框外部不能取消
        requireDialog().setCanceledOnTouchOutside(false)

        val pdBinding = ProgressDialogBinding.inflate(inflater)
        // 设置等待消息
        pdBinding.pdMessageTv.text = getString(resId, formatArgs.toTypedArray())
        return pdBinding.root
    }
}